﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zj.OpenAuth.Domain.Core;

namespace Zj.OpenAuth.Repository
{
    /// <summary>
    /// project init seed data
    /// </summary>
    public static class SeedData
    {
        public static void Initialize()
        {
            using (var context = new Models.OpenAuthDbContext())
            {
                if (context.User.Any())
                {
                    return;   // 已经初始化过数据，直接返回
                }

                //增加部门
                context.Department.AddRange(
                    new List<Department>() {
                    new Department
                   {
                       Text = "Fonour集团总部",
                       ParentId = null,
                       Code="Trunk",
                       Description="Fonour集团总部",
                       Phone="XXXXXXXXX"
                   },
                    new Department
                    {
                          Text = "Fonour集团南京分部",
                          ParentId = 1,
                          Code="Branch",
                          Description="Fonour集团南京分部",
                          Phone="XXXXXXXXX"
                    },
                    new Department
                    {
                          Text = "Fonour集团四川分部",
                          ParentId = 1,
                          Code="Branch",
                          Description="Fonour集团四川分部",
                          Phone="XXXXXXXXX"
                    }
                  }
                );
                //增加四个基本功能菜单
                context.Menu.AddRange(
                    new List<Menu>() {
                          new Menu
                   {
                       Text = "系统设置",
                       Code = "System",
                       ParentId = null,
                       Sort=0,
                       Icon = "fa fa-link",
                       Url="System/Index"
                   },
                   new Menu
                   {
                       Text = "组织机构管理",
                       Code = "Department",
                       ParentId = 1,
                       Sort=0,
                       Icon = "fa fa-link",
                       Url="Departments/Index"
                   },
                        new Menu
                   {
                       Text = "用户管理",
                       Code = "User",
                       ParentId = 1,
                       Sort = 2,
                       Icon = "fa fa-link",
                       Url="User/Index"
                   },
                   new Menu
                   {
                       Text = "角色管理",
                       Code = "Role",
                       ParentId = 1,
                       Sort = 1,
                       Icon = "fa fa-link",
                       Url="Role/Index"

                   },
                   new Menu
                   {
                       Text = "功能管理",
                       Code = "Menu",
                       Sort = 3,
                       ParentId = 1,
                       Icon = "fa fa-link",
                       Url="Menu/Index"
                   }
                   }
                );
                context.SaveChanges();

                var departmentId = context.Department.FirstOrDefault().Id;
                //增加用户
                context.User.AddRange(
                    new List<User>() {
                     new User
                     {
                         RealName = "超级管理员",
                         Password = "123456", //暂不进行加密
                         Account = "Super",
                         DepartmentId=departmentId
                     },
                     new User
                     {
                         RealName = "系统管理员",
                         Password = "123456", //暂不进行加密
                         Account = "Admin",
                         DepartmentId=departmentId
                     },
                     new User
                     {
                         RealName = "操作员",
                         Password = "123456", //暂不进行加密
                         Account = "Operator",
                         DepartmentId =departmentId
                     },
                     }
                );
                //增加一个超级管理员用户
                context.Role.AddRange(
                    new List<Role>() {
                     new Role
                     {
                         Text = "超级管理员",
                         Code = "Super"
                     },
                     new Role
                     {
                         Text = "系统管理员",
                         Code = "Admin"
                     },
                     new Role
                     {
                         Text = "操作员",
                         Code = "Operator"
                     },
                     }
                );
                context.SaveChanges();
                //user role mapping
                foreach (var user in context.User)
                {
                    foreach (var role in context.Role)
                    {
                        context.UserRoleMapping.Add(new UserRoleMapping()
                        {
                            UserId = user.Id,
                            RoleId = role.Id
                        });
                    }
                }
                //role menu mapping
                foreach (var role in context.Role)
                {
                    foreach (var menu in context.Menu)
                    {
                        context.RoleMenuMapping.Add(new RoleMenuMapping()
                        {
                            RoleId = role.Id,
                            MenuId = menu.Id,
                        });
                    }
                }
                context.SaveChanges();
            }
        }
    }
}
